/**
 * 作者:hanbo
 * 日期:2018/12/2917:49
 * 描述:
 */
define(["dojo/_base/declare", "esri/Graphic", "esri/views/2d/draw/Draw", "esri/geometry/Polyline", "esri/geometry/Point", "esri/geometry/Polygon", "esri/geometry/geometryEngine", "dojo/domReady!"], function (declare, Graphic, Draw, Polyline, Point, Polygon, geometryEngine) {
    return declare("Draw", null, {
            drawPoint: function (view, callBack) {
                // 获取焦点
                view.focus();
                view.graphics.removeAll();//清楚之前的绘制
                var draw = new Draw({
                    view: view
                });
                var action = draw.create("point", {
                    mode: "click"
                });
                // 顶点添加事件
                action.on("vertex-add", createPoint);


                //顶点移除事件
                action.on("vertex-remove", createPoint);


                // 鼠标移动事件
                action.on("cursor-update", createPoint);


                // 绘制完成事件
                action.on("draw-complete", createPoint);

                //根据点坐标生成新的线
                function createPoint(event) {
                    //获取所有顶点
                    var vertices = event.coordinates;
                    //清除之前绘制
                    view.graphics.removeAll();
                    var point = new Point({
                        hasZ: false,
                        hasM: false,
                        x: vertices[0],
                        y: vertices[1],
                        spatialReference: view.spatialReference
                    });

                    // 生成绘制的图形
                    var graphic = new Graphic({
                        geometry: point,
                        symbol: {
                            type: "simple-marker",
                            style: "square",
                            color: "blue",
                            size: "8px",
                            outline: {
                                color: [255, 255, 0],
                                width: 3
                            }
                        }
                    });
                    // 将绘制的图形添加到view
                    view.graphics.add(graphic);
                    if (event.type === "draw-complete") {
                        callBack(point);
                    }
                }
            },
            drawPolyline: function (view, callBack) {
                // 获取焦点
                view.focus();
                view.graphics.removeAll();//清楚之前的绘制
                var draw = new Draw({
                    view: view
                });
                var action = draw.create("polyline", {
                    mode: "click"
                });
                // 顶点添加事件
                action.on("vertex-add", createPolyline);


                //顶点移除事件
                action.on("vertex-remove", createPolyline);


                // 鼠标移动事件
                action.on("cursor-update", createPolyline);


                // 绘制完成事件
                action.on("draw-complete", createPolyline);

                //根据点坐标生成新的线
                function createPolyline(event) {
                    //获取所有顶点
                    var vertices = event.vertices;
                    //清除之前绘制
                    view.graphics.removeAll();
                    var line = new Polyline({
                        paths: vertices,
                        spatialReference: view.spatialReference
                    });
                    // 生成绘制的图形
                    var graphic = new Graphic({
                        geometry: line,
                        symbol: {
                            type: "simple-line",
                            color: [4, 90, 141],
                            width: 4,
                            cap: "round",
                            join: "round"
                        }
                    });
                    // 将绘制的图形添加到view
                    view.graphics.add(graphic);
                    if (event.type === "draw-complete") {
                        callBack(line);
                    }
                }
            },
            drawPolygon: function (view, callBack) {
                // 获取焦点
                view.focus();
                view.graphics.removeAll();//清楚之前的绘制
                var draw = new Draw({
                    view: view
                });
                var action = draw.create("polygon", {
                    mode: "click"
                });
                // 顶点添加事件
                action.on("vertex-add", createPolygon);


                //顶点移除事件
                action.on("vertex-remove", createPolygon);


                // 鼠标移动事件
                action.on("cursor-update", createPolygon);


                // 绘制完成事件
                action.on("draw-complete", createPolygon);

                //根据点坐标生成新的线
                function createPolygon(event) {
                    //获取所有顶点
                    var vertices = event.vertices;
                    //清除之前绘制
                    view.graphics.removeAll();
                    var polygon = new Polygon({
                        hasZ: false,
                        hasM: false,
                        rings: [vertices],
                        spatialReference: view.spatialReference
                    });
                    // 生成绘制的图形
                    var graphic = new Graphic({
                        geometry: polygon,
                        symbol: {
                            type: "simple-line",
                            color: [4, 90, 141],
                            width: 4,
                            cap: "round",
                            join: "round"
                        }
                    });
                    // 将绘制的图形添加到view
                    view.graphics.add(graphic);
                    if (event.type === "draw-complete") {
                        callBack(polygon)
                    }
                }
            },
            /**
             * 根据多边形生成缓冲区
             * @param view
             * @param geometry
             * @param distance {Number} 距离，单位米
             */
            createBuffer: function (view, geometry, distance) {
                //样式
                var polySym = {
                    type: "simple-fill", // autocasts as new SimpleFillSymbol()
                    color: [140, 140, 222, 0.5],
                    outline: {
                        color: [0, 0, 0, 0.5],
                        width: 2
                    }
                };
                var buffer = geometryEngine.buffer(geometry, distance, "meters", false);
                view.graphics.add((new Graphic({
                    geometry: buffer,
                    symbol: polySym
                })));
                return buffer;
            },

            getArea:function (geometry) {
                return geometryEngine.planarArea(geometry,"square-meters");
            }
        }
    )
});